package cn.com.surker.workcon.service.bcs;

import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.workcon.common.GKJDBCService;
import cn.com.surker.workcon.common.ProDelEnum;
import cn.com.surker.workcon.service.ProdDelService;
import cn.com.surker.workcon.service.bcs.common.BCSDelFlagEnum;
import cn.com.surker.util.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 删除派单数据 Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2024-09-26
 */
public class ProdDelServiceImpl extends GKJDBCService implements ProdDelService {

	public int insert(Long lineId, Long dispatchId, String dispatchCode) {
		if (lineId == null || dispatchId == null || StringUtils.isBlank(dispatchCode)) {
			return -1;
		}
		//	获取生产线
		SysLine line = getLine(lineId);
		if (line == null) {
			return -1;
		}
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return -1;
		}
		Long controlLineId = line.getControlLineId();
		//	插入sql
		String insertSql = "insert into ProdDel (erpId,station,code,flag) values (?,?,?,?)";
		//	拼接参数
		Object[] params = {dispatchId, controlLineId, dispatchCode, BCSDelFlagEnum.DEFAULT.getCode()};
		//	开启事务
		beginTransaction(lineId);
		//	插入数据
		int insert = lineJdbcTemp.update(insertSql, params);
		return insert;
	}

	public Integer selectByDisId(Long lineId, Long dispatchId) {
		if (lineId == null || dispatchId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		if (line == null) {
			return null;
		}
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return null;
		}
		String querySql = "select top 1 flag from ProdDel where erpId = " + dispatchId;
		//	查询状态
		String flag = lineJdbcTemp.queryForObject(querySql, String.class);
		if (StringUtils.isBlank(flag)) {
			//	查询是否存在派单记录
			String queryProduceSql = "select top 1 flag from Produce where erpId = " + dispatchId;
			List<Map<String, Object>> maps = lineJdbcTemp.queryForList(queryProduceSql);
			if (maps == null || maps.isEmpty()) {
				//	没有查询出数据,返回删除
				return ProDelEnum.SUCCESS.getCode();
			}
			return null;
		}
		int i = flag.indexOf(BCSDelFlagEnum.SUCCESS.getCode());
		if (i > -1) {
			//	删除成功
			return ProDelEnum.SUCCESS.getCode();
		} else {
			//	没有删除成功的情况下,判断是否有过操作,如果有操作,返回操作失败
			if (BCSDelFlagEnum.DEFAULT.getCode().equals(flag)) {
				return ProDelEnum.DEFAULT.getCode();
			} else {
				return ProDelEnum.FAIL.getCode();
			}
		}
	}

	public Integer deleteByDisId(Long lineId, Long dispatchId) {
		if (lineId == null || dispatchId == null) {
			return -1;
		}
		SysLine line = getLine(lineId);
		if (line == null) {
			return -1;
		}
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return -1;
		}
		//	删除sql
		String delSql = "delete from ProdDel where erpId = " + dispatchId;
		//	开启事务
		beginTransaction(lineId);
		int update = lineJdbcTemp.update(delSql);
		return update;
	}
}


